package com.ai.service.impl;

import com.ai.constant.SystemConst;
import com.ai.service.ISSOLoginKeyValidService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.SortedMap;
import java.util.TreeMap;

@Service
public class SSOLoginKeyValidServiceImpl implements ISSOLoginKeyValidService {

    @Override
    public boolean validSsoLoginKey(String sysCode, String token, String main_acct_code, String index) {
        boolean bRet = false ;
        SortedMap<String, String> dic = new TreeMap<String, String>() ;
        dic.put("transaction_id", getTransactionId(sysCode)) ;
        dic.put("token", token) ;
        dic.put("main_acct_code", main_acct_code) ;
        dic.put("index", index) ;
        dic.put("request_time", getRequestTime()) ;
        JSONObject json = new JSONObject();
        json.putAll(dic);
        String jsonData = json.toString() ;

        System.out.println("cqdxai request jsonData::"+jsonData);

        try {
            //请求的webservice的url
//            URL url = new URL(SystemConst.URL_TYRZ_TOKEN_TEST);
            URL url = new URL(SystemConst.URL_TYRZ_TOKEN_PROD);
            //创建http链接
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            //设置请求的方法类型
            httpURLConnection.setRequestMethod("POST");
            //设置请求的内容类型
            httpURLConnection.setRequestProperty("Content-type", "application/json");
            httpURLConnection.setRequestProperty("X-APP-ID", "4d52df7e60d84ba0497408352947bf11");
            httpURLConnection.setRequestProperty("X-APP-KEY", "e14cad330219369de5fd1133d7e7b427");
            httpURLConnection.setRequestProperty("X-CTG-Province-ID", "10544");
            httpURLConnection.setRequestProperty("X-CTG-VERSION", "V1.0.0");
            //设置发送数据
            httpURLConnection.setDoOutput(true);
            //设置接受数据
            httpURLConnection.setDoInput(true);
            //发送数据,使用输出流
            OutputStream outputStream = httpURLConnection.getOutputStream();
            //发送数据
            outputStream.write(jsonData.getBytes());

            // 显示结果
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println("cqdxai response line=="+line);
                JSONObject rs = JSONObject.parseObject(line);
                String rsp_code = rs.getString("rsp_code");
                if("0000".equals(rsp_code)){
                    return true;
                }
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bRet ;
//        return false;
    }

    /**
     * 获取流水号（系统编码+14位时间戳+6位序列值）
     * @param sysCode 系统编码
     * @return
     */
    public String getTransactionId(String sysCode) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Random rand = new Random();
        String result = "";
        for (int i = 0; i < 10; i++) {
            result += rand.nextInt(6);
        }
        return sysCode + sdf.format(new Date()) + result;
    }

    public String getRequestTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }
}
